package com.chanyachting.emall.action.menu;

import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.beanutils.BeanUtils;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.actions.DispatchAction;

import com.chanyachting.emall.domain.Menugroup;
import com.chanyachting.emall.domain.Opertionmenu;
import com.chanyachting.emall.form.MenugroupForm;
import com.chanyachting.emall.util.RequestUtil;

public class MenugroupAction extends DispatchAction {

	
	public ActionForward list(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		
		RequestUtil ru=new RequestUtil(request);
		
		ru.setAttribute("menugroupList", Menugroup.list());
	
		return mapping.findForward("list");
	}
	
	
	
	public ActionForward listForMember(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		
		RequestUtil ru=new RequestUtil(request);
		
		ru.setAttribute("menugroupList", Menugroup.list());
	
		return mapping.findForward("listForMember");
	}

	
	public ActionForward create(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		
		RequestUtil ru=new RequestUtil(request);
		
		if(request.getMethod().equals("POST")){
			
			MenugroupForm mgForm=(MenugroupForm)form;
			
			if(mgForm.validate(mapping, request).isEmpty()){
				
				Menugroup mg=new Menugroup();
				
				BeanUtils.copyProperties(mg, mgForm);
				
				if(mg.save()){
					
					ru.setSuccessMsg();
				}else{
					
					ru.setFailMsg();
				}
				
			}
			
		}
		
		return mapping.findForward("create");
	}

	
	
	public ActionForward update(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		
		
		RequestUtil ru=new RequestUtil(request);
		
		int id=Integer.parseInt(ru.getParameter("id", "0"));
		
		if(request.getMethod().equals("POST")){
			
			MenugroupForm mgForm=(MenugroupForm)form;
			
			if(mgForm.validate(mapping, request).isEmpty()){
				
				Menugroup mg=new Menugroup(id);
				
				BeanUtils.copyProperties(mg, mgForm);
				
				if(mg.update()){
					
					ru.setSuccessMsg();
				}else{
					
					ru.setFailMsg();
				}
			}
			
		}
		
		ru.setAttribute("menugroup", new Menugroup(id));
		
		return mapping.findForward("update");
	}

	
	public ActionForward delete(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		
		RequestUtil ru=new RequestUtil(request);
		
		int id=Integer.parseInt(ru.getParameter("id", "0"));
		
		Menugroup mg=new Menugroup(id);
		
		//检查菜单组下是否有菜单，如果有，不能删除
		
		List<Opertionmenu> list=mg.getMenuList();
		
		if(list!=null && list.size()>0){
			
			ru.setMsg("该组下有菜单项，不能删除！");
			
		}else{
			
			mg.delete();
		}
		
		return mapping.findForward("delete");
	}

	
	

}
